package vista.paneles;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vista.objetos.FabricaAtributoGraficable;
import vista.ventanas.ConstantesVista;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.Iterator;
import java.util.List;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

@SuppressWarnings("serial")
public class PanelTipoAtributo extends JPanel{
	
	private JComboBox<FabricaAtributoGraficable> comboTipo;
	private JTextField txtExpresion;
	
	public PanelTipoAtributo()
	{
		super();
		this.setBorder(BorderFactory.createTitledBorder("Tipo"));
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{110, 110, 0};
		gridBagLayout.rowHeights = new int[]{40, 0};
		gridBagLayout.columnWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		this.setLayout(gridBagLayout);
		
		agregarComboTipo();
		agregarTxtExpresion();
		escucharEventos();
	}

	/* para datos */
	public FabricaAtributoGraficable getSeleccionDeTipo()
	{	
		return (FabricaAtributoGraficable)this.comboTipo.getSelectedItem();
	}
	
	public void setSeleccionDeTipo(String seleccion) throws ElementoNoExistenteException
	{	
		for (int elementoNro = 0; elementoNro < comboTipo.getItemCount(); elementoNro++)
		{
			if (comboTipo.getItemAt(elementoNro).toString().compareTo(seleccion) == 0 )
			{
				comboTipo.setSelectedIndex(elementoNro);
				txtExpresion.setEnabled(((FabricaAtributoGraficable) comboTipo.getItemAt(elementoNro)).necesitaExpresion());
				return;
			}
		}
		
		throw new ElementoNoExistenteException("El tipo de Atributo: " + seleccion + " no esta soportado.");
	}
	
	public String getExpresion()
	{
		return (String)this.txtExpresion.getText();
	}
	
	public void setExpresion(String expresion)
	{
		this.txtExpresion.setText(expresion);
	}
	
	public void agregarTiposDeAtributo(List<FabricaAtributoGraficable> tiposAtributo)
	{
		Iterator<FabricaAtributoGraficable> atributo = tiposAtributo.iterator();
		
		while (atributo.hasNext())
			this.comboTipo.addItem(atributo.next());
	}

	/* para eventos */	
	public void setEnabled(boolean enabled)
	{		
		super.setEnabled(enabled);
		comboTipo.setEnabled(enabled);
	}
	
	private void escucharEventos() 
	{
		this.comboTipo.addPropertyChangeListener(new PropertyChangeListener()  //este para cuando se deshabilita y vuelve a habilitar
		{
			@Override
			public void propertyChange(PropertyChangeEvent arg0)
			{	
				if (comboTipo.isEnabled() == false)
					txtExpresion.setEnabled(false);
				else if (getSeleccionDeTipo() != null)
					txtExpresion.setEnabled(getSeleccionDeTipo().necesitaExpresion());
			}
		});

		this.comboTipo.addActionListener( new ActionListener() // para cuando hace clic
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{ 
				if (getSeleccionDeTipo() != null)
					txtExpresion.setEnabled(getSeleccionDeTipo().necesitaExpresion());
			}
		});
	}	

	/*para dibujar*/
	
	private void agregarTxtExpresion()
	{
		txtExpresion = new JTextField();
		
		GridBagConstraints gbc_txtExpresion = new GridBagConstraints();
		gbc_txtExpresion.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtExpresion.insets = new Insets(0, 5, 15, 0);
		gbc_txtExpresion.gridx = 1;
		gbc_txtExpresion.gridy = 0;
				
		this.add(txtExpresion, gbc_txtExpresion);
	}

	private void agregarComboTipo() 
	{
		comboTipo = new JComboBox<FabricaAtributoGraficable>();
		
		GridBagConstraints gbc_comboTipo = new GridBagConstraints();
		gbc_comboTipo.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboTipo.insets = new Insets(0, ConstantesVista.SUBPANEL_MARGEN_HORIZONTAL, ConstantesVista.SUBPANEL_MARGEN_VERTICAL, 5);
		gbc_comboTipo.gridwidth = 1;
		gbc_comboTipo.gridx = 0;
		gbc_comboTipo.gridy = 0;
		
		this.add(comboTipo, gbc_comboTipo);
	}
}